<?php

class MemberAction extends CommonAction{

	public function index(){
		$this->display();
	}

	public function logout(){
		$_SESSION[self::GLOBAL_SESSION_USER_KEY] = null;
		$this->redirect("/Index/");
	}

	public function register(){
		$Member = D("Member");
		$msgs = array();

		if($vo = $Member->create()) {
			$list=$Member->add();
			if($list!==false){
				$_SESSION[self::GLOBAL_SESSION_USER_KEY] = $Member;
			}else{
				$msgs = array("Interal error.");
				$this->assign("msgs",$msgs);
				$this->display("Member:login");
			}
		}else{
			$msgs = array($Member->getError());
			$this->assign("msgs",$msgs);
			$this->display("Member:login");
		}

		$allCategories = $this->getAllCategories();
		$this->display("Member:account_setting");
	}

	public function login(){
		$this->assign("allCategories",$this->getAllCategories());
		$this->display("Member:login");
	}

	public function login_check(){
		$Member = D("Member");
		$msgs = array();

		$conditions['email'] = $_POST["email"];
		$conditions['password'] = pwdHash($_POST["password"]);
		$result = $Member->where($conditions)->select();
		if($result == null || empty($result)){
			$msgs = array("Email or Password is incorrect.");
			$this->assign("msgs",$msgs);
			$this->getAllCategories();
			$this->display("Member:login");
		}

		// 				$params["email"] = $_POST['email'];
		// 				$this->redirect("/Member/account_setting",$params);
		$_SESSION[self::GLOBAL_SESSION_USER_KEY] = $result[0];
		$this->assign("email",$_POST['email']);
		$this->account_setting();
	}

	public function account_setting(){

		if(empty($_SESSION[self::GLOBAL_SESSION_USER_KEY])){
			$this->redirect("/Member/login");
		}


		if(empty($_POST["tab_name"])){
			$this->assign("tab_name","change_detail");
		}

		//Gender dic
		$genderDicModel = D("Dict");
		$genderDic = $genderDicModel->relation(true)->where('code = "gender"')->select();

		//Member query
		$MemberModel = D('Member');
// 		$Members = $MemberModel->where("`email`='" . $_POST['email'] . "'")->select();
		$Members = $MemberModel->where("`email`='" . $_SESSION[self::GLOBAL_SESSION_USER_KEY]['email'] . "'")->select();

		//describes dict
		$describesDicModel = D("Dict");
		$describes = $describesDicModel->relation(true)->where('code = "describes"')->select();

		//country dict
		$countryDictModel = D("Dict");
		$countries = $countryDictModel->relation(true)->where('code = "country"')->select();

		$allCategories = $this->getAllCategories();
		$this->assign("genderDic",$genderDic[0]["DictMap"]);
		$this->assign("member",$Members[0]);
		$this->assign("describes",$describes[0]["DictMap"]);
		$this->assign("countries",$countries[0]["DictMap"]);

		$this->display("Member:account_setting");

	}

	public function change_detail(){
		if(empty($_SESSION[self::GLOBAL_SESSION_USER_KEY])){
			$this->redirect("/Member/login");
		}

		$member_id = $_POST["id"];

		$updateFields["sex"] = $_POST['sex'];
		$updateFields["first_name"] = $_POST['first_name'];
		$updateFields["last_name"] = $_POST['last_name'];
		$updateFields["describes"] = $_POST['describes'];
		$updateFields["address1"] = $_POST['address1'];
		$updateFields["address2"] = $_POST['address2'];
		$updateFields["city"] = $_POST['city'];
		$updateFields["country"] = $_POST['country'];
		$updateFields["state"] = $_POST['state'];
		$updateFields["post_code"] = $_POST['post_code'];
		$updateFields["phone"] = $_POST['phone'];
		$updateFields["fax"] = $_POST['fax'];
		$updateFields["company_name"] = $_POST['company_name'];
		$updateFields["news_type"] = $_POST['news_type'];

		M("Member")->where("id=". $member_id )->save($updateFields);


		//Member query
		$MemberModel = D('Member')->getById($member_id);
		$_POST["email"] = $MemberModel["email"];
		$this->assign("tab_name","change_detail");

		// 		$params["email"] = $MemberModel["email"];
		// 		$this->redirect("/Member/account_setting",$params);
		$this->assign("email",$MemberModel["email"]);
		$_POST["email"] = $MemberModel["email"];
		$_POST["tab_name"] = $_POST['change_detail'];
		
		$_SESSION[self::GLOBAL_SESSION_USER_KEY] = $MemberModel;
		$this->account_setting();

	}

	public function change_email(){
		if(empty($_SESSION[self::GLOBAL_SESSION_USER_KEY])){
			$this->redirect("/Member/login");
		}

		$member_id = $_POST["id"];
		$password = pwdHash($_POST["origin_password"]);
		$member = D("Member")->getById($member_id);

		if($member["password"] != $password){
			$this->assign("change_email_msg","Password is incorrect.");
			$this->assign("tab_name","change_email");
			$this->assign("email",$member["email"]);
			$_POST["email"] = $member['email'];
			$_POST["tab_name"] = 'change_email';
			$this->account_setting();
		}

		if(empty($_POST['email'])){
			$this->assign("change_email_msg","Email must be not null.");
			$this->assign("tab_name","change_email");
			$this->assign("email",$member["email"]);
			$_POST["email"] = $member['email'];
			$_POST["tab_name"] = 'change_email';
			$this->account_setting();
		}

		$MemberModel = D('Member');
		//Member query
		$Members = $MemberModel->where('email=\'' . $_POST['email'] . '\'')->select();
		if($Members != null){
			$this->assign("change_email_msg","Email is exists.");
			$this->assign("tab_name","change_email");
			$this->assign("email",$member["email"]);
			$_POST["email"] = $member['email'];
			$_POST["tab_name"] = 'change_email';
			$this->account_setting();
		}

		$updateFields["email"] = $_POST['email'];
		M("Member")->where("id=". $member_id )->save($updateFields);

		$this->assign("email",$_POST['email']);
		$this->assign("tab_name","change_email");
		$_POST["email"] = $_POST['email'];
		$_POST["tab_name"] = 'change_email';
		$this->account_setting();
	}



	public function change_password(){
		if(empty($_SESSION[self::GLOBAL_SESSION_USER_KEY])){
			$this->redirect("/Member/login");
		}
		$member_id = $_POST["id"];
		$password = pwdHash($_POST["password"]);
		$repassword = pwdHash($_POST["repassword"]);
		$member = D("Member")->getById($member_id);

		if($password != $repassword){
			$this->assign("change_password_msg","Password and Repassword is not match.");
			$this->assign("tab_name","change_password");
			$_POST["email"] = $member["email"];
			$_POST["tab_name"] = $member["change_password"];
			$this->account_setting();
		}

		$origin_password = pwdHash($_POST["origin_password"]);

		dump($member);

		if($member["password"] != $origin_password){
			$this->assign("change_password_msg","Password is incorrect.");
			$this->assign("tab_name","change_password");
			$_POST["email"] = $member["email"];
			$_POST["tab_name"] = "change_password";
			$this->account_setting();
		}

		$updateFields["password"] = pwdHash($_POST['password']);
		M("Member")->where("id=". $member_id )->save($updateFields);

		// 		$params["email"] = $member["email"];
		// 		$params["tab_name"] = $member["change_email"];
		// 		$this->redirect("/Member/account_setting",$params);


		$this->assign("email",$member["email"]);
		$this->assign("tab_name","change_password");
		$_POST["email"] = $member["email"];
		$_POST["tab_name"] = "change_password";
		$this->account_setting();
	}


}

?>